package com.cloud.punish.workflow.command.assign;

import java.util.Arrays;
import java.util.Date;

import org.springframework.context.ApplicationContext;

import com.cloud.punish.common.context.HumanSession;
import com.cloud.punish.common.exception.BizException;
import com.cloud.punish.common.utils.SessionHelper;
import com.cloud.punish.workflow.command.Command;
import com.cloud.punish.workflow.command.WfRequest;
import com.cloud.punish.workflow.command.WfResponse;
import com.cloud.punish.workflow.entity.WfActInst;
import com.cloud.punish.workflow.entity.WfItemInst;
import com.cloud.punish.workflow.entity.WfPerPart;
import com.cloud.punish.workflow.enums.WfItemTypeEnum;
import com.cloud.punish.workflow.enums.WfPartEnum;
import com.cloud.punish.workflow.mapper.WfActInstMapper;
import com.cloud.punish.workflow.mapper.WfItemInstMapper;
import com.cloud.punish.workflow.mapper.WfPerPartMapper;

import tk.mybatis.mapper.entity.Example;

/**
 * 办理操作指令
 * 
 * @author taominglong
 *
 */
public class AssignCommand extends Command {

	@Override
	public void execute(WfRequest request, WfResponse resp, ApplicationContext context) {
		AssignRequest assignReq = (AssignRequest) request;
		HumanSession humanSession = SessionHelper.get();

		// 设置流程实例办理人为当前用户
		WfActInstMapper wfActInstMapper = context.getBean(WfActInstMapper.class);
		WfActInst actInst = wfActInstMapper.selectByPrimaryKey(assignReq.getActId());
		actInst.setPartId(humanSession.getHumanId());
		actInst.setPartName(humanSession.getHumanName());
		actInst.setPartType(WfPartEnum.HUMAN.getPartType());
		Example example = new Example(WfActInst.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("actId", assignReq.getActId());
		criteria.andIn("partType",
				Arrays.asList(new String[] { WfPartEnum.UNIT.getPartType(), WfPartEnum.ROLE.getPartType() }));
		int i = wfActInstMapper.updateByExampleSelective(actInst, example);
		if (1 != i) {
			throw new BizException(500, "案卷已被其他用户办理");
		}
		// 工作项表
		WfItemInstMapper wfItemInstMapper = context.getBean(WfItemInstMapper.class);
		WfItemInst itemInst = new WfItemInst();
		itemInst.setActId(actInst.getActId());
		itemInst.setActDefName(actInst.getActDefName());
		itemInst.setActionTime(new Date());
		itemInst.setActionName("办理");
		itemInst.setDetail(humanSession.getHumanName() + "在" + actInst.getActDefName() + "阶段执行办理");
		itemInst.setHumanId(humanSession.getHumanId());
		itemInst.setHumanName(humanSession.getHumanName());
		itemInst.setItemTypeId(WfItemTypeEnum.ASSIGN.getItemTypeId());
		itemInst.setItemTypeName(WfItemTypeEnum.ASSIGN.getItemTypeName());
		wfItemInstMapper.insert(itemInst);

		// 经办人表
		WfPerPartMapper wfPerPartMapper = context.getBean(WfPerPartMapper.class);
		WfPerPart perPart = new WfPerPart();
		perPart.setHumanId(humanSession.getHumanId());
		perPart.setRecId(assignReq.getRecId());
		if (!wfPerPartMapper.existsWithPrimaryKey(perPart)) {
			wfPerPartMapper.insert(perPart);
		}

	}

}
